package com.admin.education.config;

import com.admin.education.models.*;
import com.admin.education.models.Enum.CategoryType;
import com.admin.education.models.Enum.HttpMethod;
import com.admin.education.repositories.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class DbInitializer implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            ApplicationContext applicationContext = event.getApplicationContext();
            Initialize(applicationContext);
        }
    }

    public static void Initialize(ApplicationContext applicationContext) {
        UserRepository userRepository = applicationContext.getBean(UserRepository.class);
        QUser user = QUser.user;
        User entity = userRepository.selectFrom(user).where(user.userName.eq("admin")).fetchOne();
        if (null == entity) {
            entity = new User();
            entity.setPassword("123456");
            entity.setUserName("admin");

            userRepository.persist(entity);
        }
        AddScope(applicationContext);
        AddRole(applicationContext);
        AddSchools(applicationContext);
        AddCategory(applicationContext);
        AddMenu(applicationContext);
        AddIcons(applicationContext);
    }

    private static void AddRole(ApplicationContext applicationContext) {
        RoleRepository roleRepository = applicationContext.getBean(RoleRepository.class);

        QRole qRole = QRole.role;

        Role role = roleRepository.selectFrom(qRole).where(qRole.roleName.eq("在校学生")).fetchOne();
        if (null == role) {
            role = new Role();
            role.setRoleName("在校学生");
            role.setValid(true);

            roleRepository.persist(role);
        }
        role = roleRepository.selectFrom(qRole).where(qRole.roleName.eq("在校学生家长")).fetchOne();
        if (null == role) {
            role = new Role();
            role.setRoleName("在校学生家长");
            role.setValid(true);

            roleRepository.persist(role);
        }
        role = roleRepository.selectFrom(qRole).where(qRole.roleName.eq("老师")).fetchOne();
        if (null == role) {
            role = new Role();
            role.setRoleName("老师");
            role.setValid(true);

            roleRepository.persist(role);
        }
    }

    private static void AddScope(ApplicationContext applicationContext) {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
        QScopes scopes = QScopes.scopes;

        ScopesRepository scopesRepository = applicationContext.getBean(ScopesRepository.class);

        for (Object bean : beans.values()) {
            Class object = bean.getClass();
            Method[] methods = bean.getClass().getMethods();
            for (Method method : methods) {
                Scopes scope = new Scopes();

                scope.setScopeName(String.format("%s_%s", object.getName(), method.getName()));
                scope.setPath(String.format("/%s/%s", object.getName(), method.getName()));
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                PostMapping postMapping = method.getAnnotation(PostMapping.class);
                DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
                PutMapping putMapping = method.getAnnotation(PutMapping.class);
                if (null != getMapping) {
                    scope.setMethod(HttpMethod.GET);
                } else if (null != postMapping) {
                    scope.setMethod(HttpMethod.POST);
                } else if (null != deleteMapping) {
                    scope.setMethod(HttpMethod.DELETE);
                } else if (null != putMapping) {
                    scope.setMethod(HttpMethod.PUT);
                } else {
                    continue;
                }

                if (0 == scopesRepository.selectFrom(scopes).where(scopes.scopeName.eq(scope.getScopeName())).fetchCount()) {
                    scopesRepository.persist(scope);
                }
            }
        }
    }

    private static void AddSchools(ApplicationContext applicationContext) {

        QSchools qSchools = QSchools.schools;
        SchoolsRepository schoolsRepository = applicationContext.getBean(SchoolsRepository.class);

        Schools schools = schoolsRepository.selectFrom(qSchools).where(qSchools.name.eq("景贤小学")).fetchFirst();
        if (null == schools) {
            schools = new Schools();
            List<Classes> classes = new ArrayList<>();

            Classes classe = new Classes();
            classe.setCreateDate(new Date());
            classe.setName("三年二班");
            classe.setValid(true);
            classe.setSchools(schools);

            Classes classe2 = new Classes();
            classe2.setCreateDate(new Date());
            classe2.setName("三年八班");
            classe2.setValid(true);
            classe2.setSchools(schools);

            Classes classe3 = new Classes();
            classe3.setCreateDate(new Date());
            classe3.setName("三年一班");
            classe3.setValid(true);
            classe3.setSchools(schools);

            classes.add(classe);
            classes.add(classe2);
            classes.add(classe3);

            schools.setCreateDate(new Date());
            schools.setName("景贤小学");
            schools.setValid(true);

            schools.setClasses(classes);
            schoolsRepository.persist(schools);
        }
    }

    private static void AddCategory(ApplicationContext applicationContext) {
        QCategorys qCategorys = QCategorys.categorys;
        CategorysRepository categorysRepository = applicationContext.getBean(CategorysRepository.class);

        Categorys categorys = categorysRepository.selectFrom(qCategorys).where(qCategorys.name.eq("阅读考级")).fetchOne();
        if (null == categorys) {
            categorys = new Categorys();
            categorys.setName("阅读考级");
            categorys.setValid(true);
            categorys.setCreateTime(new Date());
            categorys.setCategoryType(CategoryType.Templates);

            categorysRepository.persist(categorys);
        }
        categorys = categorysRepository.selectFrom(qCategorys).where(qCategorys.name.eq("诗词阅关")).fetchOne();
        if (null == categorys) {
            categorys = new Categorys();
            categorys.setName("诗词阅关");
            categorys.setValid(true);
            categorys.setCreateTime(new Date());
            categorys.setCategoryType(CategoryType.Templates);

            categorysRepository.persist(categorys);
        }
        categorys = categorysRepository.selectFrom(qCategorys).where(qCategorys.name.eq("名著共读")).fetchOne();
        if (null == categorys) {
            categorys = new Categorys();
            categorys.setName("名著共读");
            categorys.setValid(true);
            categorys.setCreateTime(new Date());
            categorys.setCategoryType(CategoryType.Circles);

            categorysRepository.persist(categorys);
        }
        categorys = categorysRepository.selectFrom(qCategorys).where(qCategorys.name.eq("朗读分享")).fetchOne();
        if (null == categorys) {
            categorys = new Categorys();
            categorys.setName("朗读分享");
            categorys.setValid(true);
            categorys.setCreateTime(new Date());
            categorys.setCategoryType(CategoryType.Circles);

            categorysRepository.persist(categorys);
        }
        categorys = categorysRepository.selectFrom(qCategorys).where(qCategorys.name.eq("刺绣作品")).fetchOne();
        if (null == categorys) {
            categorys = new Categorys();
            categorys.setName("刺绣作品");
            categorys.setValid(true);
            categorys.setCreateTime(new Date());
            categorys.setCategoryType(CategoryType.Circles);

            categorysRepository.persist(categorys);
        }
    }

    private static void AddMenu(ApplicationContext applicationContext) {
        MenusRepository menusRepository = applicationContext.getBean(MenusRepository.class);
        QMenus qMenus = QMenus.menus;

        Menus menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("系统管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("01");
            menus.setMenuName("系统管理");
            menus.setMenuOrder(1);
            menus.setUrl("#");
            menus.setParentMenuCode("");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("资源管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("02");
            menus.setMenuName("资源管理");
            menus.setMenuOrder(2);
            menus.setUrl("#");
            menus.setParentMenuCode("");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("题库管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0201");
            menus.setMenuName("题库管理");
            menus.setMenuOrder(1);
            menus.setUrl("#/QuestionPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("模板管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0202");
            menus.setMenuName("模板管理");
            menus.setMenuOrder(2);
            menus.setUrl("#/TemplatePage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("模板管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0202");
            menus.setMenuName("模板管理");
            menus.setMenuOrder(2);
            menus.setUrl("#/TemplatePage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("班级管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0203");
            menus.setMenuName("班级管理");
            menus.setMenuOrder(3);
            menus.setUrl("#/ClassPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("学校管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0204");
            menus.setMenuName("学校管理");
            menus.setMenuOrder(4);
            menus.setUrl("#/SchoolPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("会员管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0205");
            menus.setMenuName("会员管理");
            menus.setMenuOrder(5);
            menus.setUrl("#/ClientPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("赞助")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0206");
            menus.setMenuName("赞助");
            menus.setMenuOrder(6);
            menus.setUrl("#/SponsorPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("活动管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0207");
            menus.setMenuName("活动管理");
            menus.setMenuOrder(7);
            menus.setUrl("#/ActivityPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("科目管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0208");
            menus.setMenuName("科目管理");
            menus.setMenuOrder(8);
            menus.setUrl("#/SubjectPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("分类管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0209");
            menus.setMenuName("分类管理");
            menus.setMenuOrder(9);
            menus.setUrl("#/CategoryPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("图标管理")).fetchOne();
        if (null == menus) {
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0210");
            menus.setMenuName("图标管理");
            menus.setMenuOrder(10);
            menus.setUrl("#/IconPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("积分规则")).fetchOne();
        if(null==menus){
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0211");
            menus.setMenuName("积分规则");
            menus.setMenuOrder(11);
            menus.setUrl("#/DesignationsPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

//        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("文章管理")).fetchOne();
//        if(null==menus){
//            menus = new Menus();
//
//            menus.setCulture("zh-CN");
//            menus.setMenuCode("0212");
//            menus.setMenuName("文章管理");
//            menus.setMenuOrder(12);
//            menus.setUrl("#/ArticlePage/IndexPage");
//            menus.setParentMenuCode("02");
//
//            menusRepository.persist(menus);
//        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("投票管理")).fetchOne();
        if(null==menus){
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0213");
            menus.setMenuName("投票管理");
            menus.setMenuOrder(13);
            menus.setUrl("#/VotesPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }

        menus = menusRepository.selectFrom(qMenus).where(qMenus.menuName.eq("年级管理")).fetchOne();
        if(null==menus){
            menus = new Menus();

            menus.setCulture("zh-CN");
            menus.setMenuCode("0214");
            menus.setMenuName("年级管理");
            menus.setMenuOrder(14);
            menus.setUrl("#/GradesPage/IndexPage");
            menus.setParentMenuCode("02");

            menusRepository.persist(menus);
        }
    }

    private static void AddIcons(ApplicationContext applicationContext) {
        IconsRepository iconsRepository = applicationContext.getBean(IconsRepository.class);
        QIcons qIcons = QIcons.icons;

        Icons icons = iconsRepository.selectFrom(qIcons).where(qIcons.name.eq("活动公告")).fetchFirst();
        if (null == icons) {
            icons = new Icons();
            icons.setName("活动公告");
            icons.setImgPath("");
            icons.setEndPoint("");

            iconsRepository.persist(icons);
        }
        icons = iconsRepository.selectFrom(qIcons).where(qIcons.name.eq("荣誉榜单")).fetchFirst();
        if (null == icons) {
            icons = new Icons();
            icons.setName("荣誉榜单");
            icons.setImgPath("");
            icons.setEndPoint("");

            iconsRepository.persist(icons);
        }
        icons = iconsRepository.selectFrom(qIcons).where(qIcons.name.eq("我来挑战")).fetchFirst();
        if (null == icons) {
            icons = new Icons();
            icons.setName("我来挑战");
            icons.setImgPath("");
            icons.setEndPoint("");

            iconsRepository.persist(icons);
        }
        icons = iconsRepository.selectFrom(qIcons).where(qIcons.name.eq("成长印记")).fetchFirst();
        if (null == icons) {
            icons = new Icons();
            icons.setName("成长印记");
            icons.setImgPath("");
            icons.setEndPoint("");

            iconsRepository.persist(icons);
        }
    }

}

